package com.model.user;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.OneToOne;
import javax.persistence.TableGenerator;

import com.core.protocols.IKVObject;
import com.core.utils.KeyValueDictionary;
import com.core.utils.LocalizableString;
import com.model.KeyValue;

@Entity(name = "USER")
public class User
        implements IKVObject {

	private Integer      id;
	private String       name;
	private String       description;
	private String       mailAddress;
	private String       passwordHashString;
	private UserCategory userCategory = new UserCategory();



	@Id
	@TableGenerator(allocationSize = 10, table = "Generators", pkColumnName = "generatorName", pkColumnValue = "generatorUser", name = "generatorUser")
	@GeneratedValue(strategy = GenerationType.TABLE, generator = "generatorUser")
	public Integer getId () {

		return id;
	}



	public void setId ( Integer id ) {

		this.id = id;
	}



	public String getName () {

		return name;
	}



	public void setName ( String name ) {

		this.name = name;
	}



	@Lob
	public String getDescription () {

		return description;
	}



	public String getPasswordHashString () {

		return passwordHashString;
	}



	private String passwordMD5String ( String password ) {

		return com.core.utils.StringUtils.md5FromString(password);

	}



	public void applyPasswordMD5String ( String password ) {

		this.passwordHashString = this.passwordMD5String(password);
	}



	public void setPasswordHashString ( String passwordHashString ) {

		this.passwordHashString = passwordHashString;
	}



	public void setDescription ( String description ) {

		this.description = description;
	}



	@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	public UserCategory getUserCategory () {

		return userCategory;
	}



	public void setUserCategory ( UserCategory userCategory ) {

		this.userCategory = userCategory;
	}



	public String getMailAddress () {

		return mailAddress;
	}



	public void setMailAddress ( String mailAddress ) {

		this.mailAddress = mailAddress;
	}



	/*
	 * KVO
	 */
	public KeyValueDictionary toDictionary () {

		KeyValueDictionary result = new KeyValueDictionary();
		return result;
	}



	@Override
	public void assignTo ( IKVObject object ) {

		User user = (User) object;
		user.setDescription(this.getDescription());
		user.setName(this.getName());
		user.setMailAddress(this.getMailAddress());
		user.getUserCategory().setUserGroup(this.getUserCategory().getUserGroup());
		user.getUserCategory().setUserRole(this.getUserCategory().getUserRole());

	}



	@Override
	public KeyValue idValue () {

		String strIdValue = this.getId().toString();
		return new KeyValue(this.fieldNameAsId(), strIdValue, false);

	}



	@Override
	public String fieldNameAsId () {

		return "id";
	}



	@Override
	public KeyValue nameValue () {

		String strIdValue = this.getName().toString();
		return new KeyValue(this.fieldNameAsName(), strIdValue, false);

	}



	@Override
	public String fieldNameAsName () {

		return "name";
	}



	public String localizableDescription () {

		return LocalizableString.sharedInstance().localizable("entity.User.description");
	}
}
